/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package treccontextsug;

/**
 *
 * @author ditusp
 */

import java.io.File;
import java.io.IOException;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;
import org.codehaus.jackson.JsonEncoding;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.map.JsonMappingException;

public class JSONRead {

   public static void main(String[] args) throws IOException {
 
	JsonFactory jfactory = new JsonFactory();
 
	/*** read from file ***/
	JsonParser jParser = jfactory.createJsonParser(new File("/home/ditusp/Desktop/database.json"));
 
	// loop until token equal to "}"
	while (jParser.nextToken() != JsonToken.END_OBJECT) {
 
                String fieldname = jParser.getCurrentName();
 
		  jParser.nextToken(); // current token is "{", move next
                  attraction.setCity(jParser.getText());
                               
		  // messages is array, loop until token equal to "]"
		  while (jParser.nextToken() != JsonToken.END_ARRAY) {
                        
                      attraction.setDocid(jParser.getText());
                       
                      if (attraction.docid != null) {
                                attraction.setDescription(jParser.getText());
                        }
                            else {
                          // current token is "docid",
                          jParser.nextToken();
                          attraction.setDocid(jParser.getText());

                          // current token is "url", 
                          jParser.nextToken();
                          attraction.setUrl(jParser.getText());
                          
                          // current token is "url", 
                          jParser.nextToken();
                          attraction.setTimestamp(jParser.getText());
                          
                          jParser.nextToken();

                          // current token is "description", 
                          jParser.nextToken(); 
                          attraction.setDescription(jParser.getText());
                        }
                  }
               }   
	  
          
	/*** write to file ***/
	JsonGenerator jGenerator = jfactory.createJsonGenerator(new File(
			"/home/ditusp/Desktop/user.json"), JsonEncoding.UTF8);
	jGenerator.writeStartObject(); // {
 
	jGenerator.writeFieldName(attraction.city); // "city" :
	jGenerator.writeStartArray(); // [
            jGenerator.writeString(attraction.docid);
            jGenerator.writeString(attraction.url);
            jGenerator.writeString(attraction.timestamp);
                jGenerator.writeStartArray(); // [
                    while (jParser.nextToken() != JsonToken.END_OBJECT) {
                        jGenerator.writeString(attraction.description);
                    }
                jGenerator.writeEndArray(); // ]
	jGenerator.writeEndArray(); // ]
	jGenerator.writeEndObject(); // }
	jGenerator.close();
        
     } 
}
